package q1669_mergeInBetween;

import CommonClass.ListNode;

public class Solution {
    /*
    分为两个步骤
    1 首先找到a的前一个节点位置 将链表切断 并记录切断位置的后一个节点cut 并将list2接到切断的位置
    2 然后将cut移动到b的后一个位置 将list2的最后接上cut
     */
    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode head = new ListNode(0, list1);
        ListNode temp = head;
        int index = 0;
        while (index < a) {
            temp = temp.next;
            ++index;
        }
        ListNode cut = temp.next;
        temp.next = list2;

        while (temp.next != null) temp = temp.next;

        while (index <= b) {
            cut = cut.next;
            ++index;
        }

        temp.next = cut;
        return head.next;
    }
}
